package com.vitaf.cash.app.utils.manager

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager

/**
 * Fragment导航器
 */
class VitafFragmentNavigator(private val fragmentManager: FragmentManager) {

    /**
     * 添加Fragment到容器
     * @param containerId 容器ID
     * @param fragment 要添加的Fragment
     * @param tag Fragment的标签
     * @param addToBackStack 是否添加到返回栈
     */
    fun addFragment(containerId: Int, fragment: Fragment, tag: String? = null, addToBackStack: Boolean = false) {
        if (fragment.isAdded) return

        fragmentManager.beginTransaction()
            .add(containerId, fragment,tag) // 此处的R.id.fragment_container是要盛放fragment的父容器
            .commitNow()
    }

    /**
     * 替换容器中的Fragment
     * @param containerId 容器ID
     * @param fragment 要替换的Fragment
     * @param tag Fragment的标签
     * @param addToBackStack 是否添加到返回栈
     */
    fun replaceFragment(containerId: Int, fragment: Fragment, tag: String? = null, addToBackStack: Boolean = true) {
        fragmentManager.beginTransaction()
            .replace(containerId, fragment, tag)
            .commitNow()
    }

    /**
     * 显示Fragment
     * @param fragment 要显示的Fragment
     */
    fun showFragment(fragment: Fragment) {
        if (!fragment.isHidden) return

        fragmentManager.beginTransaction()
            .show(fragment)
            .commitNow()
    }

    /**
     * 隐藏Fragment
     * @param fragment 要隐藏的Fragment
     */
    fun hideFragment(fragment: Fragment) {
        if (fragment.isHidden) return
        fragmentManager.beginTransaction()
            .hide(fragment)
            .commitNow()
    }

    /**
     * 移除Fragment
     * @param fragment 要移除的Fragment
     */
    fun removeFragment(fragment: Fragment) {
        if (!fragment.isAdded) return

        fragmentManager.beginTransaction()
            .remove(fragment)
            .commitNow()
    }

    /**
     * 查找Fragment
     * @param tag Fragment的标签
     * @return 找到的Fragment，未找到返回null
     */
    fun findFragmentByTag(tag: String): Fragment? {
        return fragmentManager.findFragmentByTag(tag)
    }

    /**
     * 返回上一个Fragment
     * @return 是否成功返回
     */
    fun popBackStack(): Boolean {
        return if (fragmentManager.backStackEntryCount > 0) {
            fragmentManager.popBackStack()
            true
        } else {
            false
        }
    }

    /**
     * 返回到指定的Fragment
     * @param tag 目标Fragment的标签
     * @param inclusive 是否包含目标Fragment
     */
    fun popBackStackToTag(tag: String, inclusive: Boolean = false) {
        fragmentManager.popBackStack(tag, if (inclusive) FragmentManager.POP_BACK_STACK_INCLUSIVE else 0)
    }

    /**
     * 隐藏所有Fragment
     */
    fun hideAllFragments(){
        val fragments = fragmentManager.fragments
        if (fragments.isNotEmpty()) {
            val transaction = fragmentManager.beginTransaction()
            fragments.forEach {
                if (!it.isHidden){
                    transaction.hide(it)
                }
            }
            transaction.commitNow()
        }
    }


    /**
     * 清除所有Fragment
     */
    fun clearAllFragments() {
        val fragments = fragmentManager.fragments
        if (fragments.isNotEmpty()) {

            fragmentManager.beginTransaction().apply {
                fragments.forEach {
                    remove(it)
                    commitNow()
                }
            }
        }
    }
}